<?php 
class useraClassAction extends ActionManage
{
	public function defaultAction()
	{
		
	}
	
	public function getxings($str)
	{
		if(!isempt($str))$str = substr($str,0,3).'*****'.substr($str,-4);
		return $str;
	}
	
	public function dataAction()
	{
		$limit= (int)$this->get('limit','1');
		$sort = $this->get('sort');
		$dir  = $this->get('dir');
		$order= '`sort` asc';
		if($sort && $dir)$order= '`'.$sort.'` '.$dir.'';
		$barr = m('usera')->getlimit('`cid`='.$this->companyid.'', (int)$this->get('page','1'),'*',$order, $limit);
		$rows  = $barr['rows'];
		$total = count($rows);
		
		foreach($rows as $k=>$rs){
			$rows[$k]['mobile'] = $this->getxings($rs['mobile']);
			if($rs['status']=='2')$rows[$k]['ishui']=1;
		}
		
		$barr = array(
			'rows' 		=> $rows,
			'totalCount'=> $barr['count'],
			'success' 	=> true,
		);
		return $barr;
	}
	
	public function formAction()
	{
		$id = (int)$this->get('id','0');
		$this->smartydata['id'] = $id;
			
		$fields = c('fields')->getArr('usera');
		$rs 	= $fields['data'];
		$fobj 	= $fields['fieldsobj'];
		
		if($id>0){
			$rs 	= m('usera')->getone('`id`='.$id.'');
		}
		
		$this->assign('fieldsobj', $fobj);
		$this->assign('data', $rs);

	}
	
	public function savecheckAction()
	{
		$name = $this->post('name');
		$user = $this->post('user');
		$mobile = $this->post('mobile');
		$type = $this->post('type');
		$id   = (int)$this->post('id','0');
		$dbs  = m('usera');
		if($dbs->rows("`cid`='{$this->companyid}' and `user`='$user' and `id`<>'$id'")>0)return '“'.$user.'”用户名已存在';
		if($dbs->rows("`cid`='{$this->companyid}' and `mobile`='$mobile' and `id`<>'$id'")>0)return '“'.$mobile.'”手机号已存在';
		
		if($type==2){
			if($id==0)return '不允许选单位创建人类型';
			$nrs = $dbs->getone($id);
			if($nrs['uid']!=$this->companyinfo['uid'])return '此用户不是单位创建人';
		}
		$ushu = $dbs->rows('`cid`='.$this->companyid.'');
		
		if($id==0){
			$uarr['mobile'] 	= $mobile;
			$flaskm = floatval($this->companyinfo['flaskm']);
			if($ushu>=$flaskm)return '已超过'.$flaskm.'用户容量';
		}else{
			$nrs = $dbs->getone($id);
			if($nrs['uid']=='0')$uarr['mobile'] 	= $mobile;
		}
		
		$uarr['name'] 		= $name;
		$uarr['user'] 		= $user;
		$uarr['cid'] 		= $this->companyid;
		$uarr['position'] 	= $this->post('position');
		$uarr['email'] 		= $this->post('email');
		$uarr['deptname'] 	= $this->post('deptname');
		$uarr['deptid'] 	= (int)$this->post('deptid','0');
		$uarr['superman'] 	= $this->post('superman');
		$uarr['superid'] 	= $this->post('superid');
		$uarr['tel'] 		= $this->post('tel');
		$uarr['sort'] 		= (int)$this->post('sort','0');
		$uarr['gender'] 	= (int)$this->post('gender','0');
		$uarr['pingyin'] = $this->post('pingyin');
		$uarr['type'] 	 = $type;
		
		$uarr['optdt'] 	 = $this->now;
		if($id==0){
			$uarr['createdt'] = $this->now;
			$dbs->insert($uarr);
			$ushu++;
		}else{
			$dbs->update($uarr,'`cid`='.$this->companyid.' and `id`='.$id.'');
		}

		m('company')->update('`flasks`='.$ushu.'', $this->companyid);
		
		return 'ok';
	}
	
	public function changeAction()
	{
		$id  = (int)$this->get('id','0');
		$zt  = (int)$this->get('zt','0');
		$ars = m('usera')->getone($id);
		$event = '';
		if($zt==0){
			if($ars['uid']>'0'){
				$zt = '1';
				$event = 'yes';
			}else{
				$event = 'cancel';
			}
		}else{
			$event = 'stop';
		}
		m('hdiao')->send($ars['cid'],'subscribe',$event, array('user' => $ars['user']));
		m('usera')->update('`status`='.$zt.'', $id);
	}
	
	public function deluseraAction()
	{
		$id = (int)$this->post('id','0');
		$where = '`cid`='.$this->companyid.' and `id`='.$id.'';
		$urs   = m('usera')->getone($where);
		if($urs){
			if($urs['uid']==$this->adminid)return returnerror('创建人不能删除');
			m('usera')->delete($where);
		}
		return returnsuccess();
	}
}